//- Copyright (C) 2018-2022 Chatopera Inc, <https://www.chatopera.com>
//- 
//- Licensed under the Apache License, Version 2.0 (the "License");
//- you may not use this file except in compliance with the License.
//- You may obtain a copy of the License at
//- 
//-  http://www.apache.org/licenses/LICENSE-2.0
//- 
//-  Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
.row
    .col-lg-12
        h1.site-h1
            | 对话列表
            form.layui-form#searchConversation(name='search', method='post')
                .layui-form-item
                    .layui-inline(style='margin-right:0px;width: 100%')
                        select.ukefu-input(name='skill', lay-ignore, style='width: 90px;display: inline-block', onchange='renderAgentsByOrgan(this.value)')
                            option(value) 所有技能组
                                if skillGroups
                                    for tpskill in skillGroups
                                        option(value=tpskill.id,selected=(skill && skill == tpskill.id?'selected':false))
                                            | #{tpskill.name}
                        label ~
                        select#agent.ukefu-input(name='agentno', lay-ignore, style='width: 100px;display: inline-block')
                            option(value) 请选择技能组
                        .layui-input-inline(style='float: right;width: auto;margin-right: 0px')
                            button.layui-btn.layui-btn-small.layui-btn-original(style='color:#ffffff;')
                                i.layui-icon 
        ul.dialog-list#chat_users
            if agentUserList
                for agentuser in agentUserList
                    li(class={
                        'clearfix': true,
                        'chat-list-item': true,
                        'active': (curagentuser.id == agentuser.id)
                    }, id='agentuser_' + agentuser.userid, remove-id=agentuser.id, data-id=agentuser.userid)
                        a(href='/apps/cca/agentuser.html?id=' + agentuser.id + '&channel=' + agentuser.channel, data-toggle="load", data-target="#ukefu-chat-agent", onclick="$('.chat-list-item.active').removeClass('active');$(this).closest('li.chat-list-item').addClass('active');cleanTopMsgTip(this) ;$('#last_msg_#{agentuser.userid}').text(0).hide();")
                            img(src=(agentuser.headimgurl && agentuser.headimgurl != '' ? agentuser.headimgurl : '/images/im/user.png'), style="width:45px;height:45px;")
                            if agentuser.status && agentuser.status == 'end'
                                if agentuser.channel && agentuser.channel == "weixin"
                                    i.kfont.ukefu-channel-icon-end(id="tip_icon_wenxin_#{agentuser.userid}")
                                        | &#xe64d;
                                else if agentuser.channel && agentuser.channel == "webim"
                                    i.kfont.ukefu-channel-icon-end(id="tip_icon_webim_#{agentuser.userid}")
                                        | &#xe60d;
                                else if agentuser.channel && agentuser.channel == "skype"
                                    i.csfont.ukefu-channel-icon-end(id="tip_icon_skype_#{agentuser.userid}")
                                        | &#59404;
                                else if agentuser.channel && agentuser.channel == "messenger"
                                    i.csfont.ukefu-channel-icon-end(id="tip_icon_messenger_#{agentuser.userid}")
                                        | &#xec11;
                                else if agentuser.channel && agentuser.channel == "phone"
                                    img.ukefu-channel-image(src="/images/cde-ico-gray.png", id="tip_icon_phone_#{agentuser.userid}")
                            else
                                if agentuser.channel && agentuser.channel == "weixin"
                                    i.kfont.ukefu-channel-icon(id="tip_icon_wenxin_#{agentuser.userid}")
                                        | &#xe64d;
                                else if agentuser.channel && agentuser.channel == "webim"
                                    i.kfont.ukefu-channel-icon(id="tip_icon_webim_#{agentuser.userid}")
                                        | &#xe60d;
                                else if agentuser.channel && agentuser.channel == "skype"
                                    i.csfont.ukefu-channel-icon(id="tip_icon_skype_#{agentuser.userid}")
                                        | &#59404;
                                else if agentuser.channel && agentuser.channel == "messenger"
                                    i.csfont.ukefu-channel-icon(id="tip_icon_messenger_#{agentuser.userid}")
                                        | &#xec11;
                                else if agentuser.channel && agentuser.channel == "phone"
                                    img.ukefu-channel-image(src="/images/phone-ico.png", id="tip_icon_phone_#{agentuser.userid}")
                            .dialog-info
                                .address
                                    span(style="width:90px;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;word-wrap: break-word;word-break: break-all;")
                                        | #{agentuser.username}
                                .news
                                    | #{pugHelper.formatDate('yyyy-MM-dd HH:mm:ss', agentuser.servicetime)}
                                    if agentuser.status && agentuser.status == 'end'
                                        small.label.bg-gray.pull-right(id="tip_message_#{agentuser.userid}") 离开
                                    else
                                        small.label.bg-green.pull-right(id="tip_message_#{agentuser.userid}") 在线
                            .last-msg
                                small.ukefu-badge.bg-red(id="last_msg_#{agentuser.userid}",style="#{(agentuser.tokenum == 0 || (curagentuser && curagentuser.id == agentuser.id)) ? 'display:none' : ''}")
                                    | #{agentuser.tokenum ? agentuser.tokenum : 0}
script(src='/js/CSKeFu_Rest_Request.v1.js')
script.
    function renderAgentsByOrgan(organid) {
        if (organid.length > 0) {
            restApiRequest({
                silent: true,
                path: 'user',
                data: {
                    ops: 'findByOrgan',
                    organ: organid
                }
            }).then(function (result) {
                if (result.rc == 0) {
                    $("#agent").html('<option value="">所有坐席</option>');
                    for (var i = 0; i < result.data.length; i++) {
                        $("#agent").prepend('<option value="' + result.data[i].id + '">' + result.data[i].uname + '</option>')
                    }
                    ;
                }
            }, function (error) {
                // 服务器异常
                top.layer.msg('服务器抽风，请稍后再试！', {icon: 2, offset: 'b', time: 3000})
            })
        } else {
            $("#agent").html("<option value=\"\">请选择技能组</option>");
        }

    }

    $(function () {
        $('#searchConversation').on('submit', function (e) {
            e.preventDefault();  //prevent form from submitting
            var data = $("#searchConversation :input").serializeArray();
            console.log(data)
            var params = {};
            for (var index = 0; index < data.length; index++) {
                params[data[index]['name']] = data[index]["value"];
            }
            $.ajax({
                url: '/apps/cca/query',
                data: {skill: params["skill"], agentno: params["agentno"]},
                type: "post",
                success: function (data) {
                    $("#chat_users").html(data);
                    if ($("#chat_users li").length > 0) {
                        if ($("#chat_users li:first-child").attr('id').substring(10) !== cursession) {
                            $("#chat_users li:first-child a").click();
                        } else {
                            $("#chat_users li:first-child").addClass("active")
                        }
                    } else {
                        cursession = "";
                        $("#ukefu-chat-agent").html(" <div style=\"height: 100%;background: #fff;padding-top: 100px\" class=\"box-body ukefu-im-theme\"><div class=\"ukefu-empty\"><i class=\"layui-icon\">&#xe63a;</i><div style=\"\">没有搜索结果</div>\</div>\</div>");
                    }
                }
            });
        });
    });
            
